package io.renren.modules.carinsurance.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.utils.JacksonUtils;
import io.renren.common.utils.PageData;
import io.renren.modules.carinsurance.dto.CarInsuranceDTO;
import io.renren.modules.carinsurance.vo.CarInsuranceVO;
import io.renren.modules.carinsurance.vo.DealInsuranceVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;



import io.renren.modules.carinsurance.dao.CarInsuranceDao;
import io.renren.modules.carinsurance.entity.CarInsuranceEntity;
import io.renren.modules.carinsurance.service.CarInsuranceService;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Service("carInsuranceService")
public class CarInsuranceServiceImpl extends ServiceImpl<CarInsuranceDao, CarInsuranceEntity> implements CarInsuranceService {
    @Override
    public PageData<CarInsuranceVO> queryInsurancePage(Map<String, Object> params) {
        log.info("车辆保险类型信息(分页)");
        // 获取信息
        String carInsuranceType = (String) params.get("carInsuranceType");
        Integer page = Integer.valueOf((String) params.get("page"));
        Integer limit = Integer.valueOf((String) params.get("limit"));
        QueryWrapper<CarInsuranceEntity> wrapper = new QueryWrapper<CarInsuranceEntity>();
        if (StringUtils.isNotBlank(carInsuranceType)) {
            wrapper.eq("car_insurance_type",carInsuranceType);
        }
        // 创建分页对象
        IPage<CarInsuranceEntity> iPage = new Page<CarInsuranceEntity>(page,limit);
        this.baseMapper.selectPage(iPage,wrapper);
        // 分页集合
        List<CarInsuranceEntity> records = iPage.getRecords();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<CarInsuranceVO> carInsuranceVOList = new ArrayList<>();
        for (CarInsuranceEntity record : records) {
            // 上市时间转换
            CarInsuranceVO carInsuranceVO = JacksonUtils.obj2pojo(record, CarInsuranceVO.class);
            // 创建时间
            carInsuranceVO.setUpdateTime(simpleDateFormat.format(new Date(record.getUpdateTime())));
            // 更新时间
            carInsuranceVO.setCreateTime(simpleDateFormat.format(new Date(record.getCreateTime())));
            carInsuranceVOList.add(carInsuranceVO);
        }
        // 总数
        long total = iPage.getTotal();
        PageData<CarInsuranceVO> pageData = new PageData<CarInsuranceVO>(carInsuranceVOList,total);
        return pageData;
    }

    @Override
    public void saveCarInsurance(CarInsuranceDTO dto) {
        log.info("新增车辆保险类型");
        CarInsuranceEntity entity = JacksonUtils.obj2pojo(dto, CarInsuranceEntity.class);
        this.baseMapper.insert(entity);
    }

    @Override
    public void editById(CarInsuranceDTO dto) {
        log.info("修改车辆保险类型");
        CarInsuranceEntity entity = JacksonUtils.obj2pojo(dto, CarInsuranceEntity.class);
        this.baseMapper.updateById(entity);
    }

    @Override
    public void deleteByIds(List<Long> asList) {
        log.info("删除车辆保险类型");
        this.baseMapper.deleteBatchIds(asList);
    }

    @Override
    public List<DealInsuranceVO> getInsuranceName() {
        log.info("获取车辆保险类型");
        List<DealInsuranceVO> dealInsuranceVOList = new ArrayList<>();
        List<CarInsuranceEntity> carInsuranceEntities = this.baseMapper.selectList(null);
        for (CarInsuranceEntity carInsuranceEntity : carInsuranceEntities) {
            DealInsuranceVO dealInsuranceVO = new DealInsuranceVO();
            dealInsuranceVO.setId(carInsuranceEntity.getId());
            dealInsuranceVO.setInsuranceName(carInsuranceEntity.getCarInsuranceType());
            dealInsuranceVOList.add(dealInsuranceVO);
        }
        return dealInsuranceVOList;
    }
}
